﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Game.DataAccess.DTO;
using Game.DataAccess.Factory;
using Game.Utilities;
using Game.Utilities.Session;
using Item = Game.DataAccess.DTO.Item;


namespace Web.Server.Controllers
{
    public class GameLogicHandler
    {
        public long BetInput( int isFree,int roomId, string betline, ref long betLogId, int serviceid)
        {
            //step 1
            int betMoney = 0;
            AbstractDaoFactory.Instance().CreateSlot().InsertBetLog((int)AccountSession.AccountID, roomId, betline, isFree, "", ref betMoney, ref betLogId);

            if (betLogId < 0)
                return -1;
            //step2
            long requestInput = 0;
            AbstractDaoFactory.Instance().CreateSlot().InsertChangingLog_Input(betLogId, (int)AccountSession.AccountID, AccountSession.AccountName, betMoney, 0, 0, "", "insert trừ tiền  -changmoney ( so tien)", 0, ref requestInput);
            if (requestInput < 0)
                return -2;
            //step Tru Tien Nguowi Choi

            //long balance = 0;
            //long responseStatus = 0;
            //AbstractDaoFactory.Instance().CreateSlot().SlotMachine_Create(serviceid, AccountSession.AccessToken, (int)AccountSession.AccountID, AccountSession.AccountName, ConvertRoomMoney(roomId), betMoney, ref balance, betLogId, "", betline, ref responseStatus);

            //if (responseStatus < 0)
            //    return responseStatus;

            //step4 
            int returnvalue = 0;
            AbstractDaoFactory.Instance().CreateSlot().UpdateChangingLog_Input(requestInput, 1, 0, "Trừ tiền thành công", 1, ref returnvalue);
            if (returnvalue < 0)
                return -3;
            
            return returnvalue;

        }

        public ResultGame BetOutput(long betLogId, int roomid, int serviceid, ref long responseOut)
        {
            int sumBetMoney = 0;
            int sumWinMoney = 0;
            int winMoneyDigGold = 0;
            int countLineWin = 0;
            string bonnusMiniGame = string.Empty;
            string resultString = string.Empty;
            int isFreeb = 0;
           
            int response = 0;
           
            var listWin = AbstractDaoFactory.Instance().CreateSlot().GetWinnerInfo(betLogId, ref isFreeb, ref sumBetMoney, ref sumWinMoney, ref winMoneyDigGold, ref countLineWin, ref bonnusMiniGame, ref resultString, ref response);

            if (response <0)
            {
                responseOut = -1;
                return null;
            }
            //step6
            int changeMoney = 0;
            long requestOuput = 0;
            AbstractDaoFactory.Instance().CreateSlot().InsertChangingLog_Output(betLogId, (int)AccountSession.AccountID, AccountSession.AccountName, ref changeMoney, 0, "", "sert api + tien " + sumWinMoney,0, ref requestOuput);

            if (requestOuput < 0)
            {
                responseOut = -2;
                return null;
            }

            //API + tien

            //long balance=0;
            //long responseStatus=0;
            //AbstractDaoFactory.Instance().CreateSlot().SlotMachine_Award(serviceid, AccountSession.AccessToken, (int)AccountSession.AccountID, AccountSession.AccountName, ConvertRoomMoney(roomid), sumWinMoney, ref balance, betLogId,"", resultString,ref responseStatus);

            //if (responseStatus < 0)
            //{
            //    responseOut = responseStatus;
            //    return null;
            //}
            
           
            //step 8

            int returnvalueOut = 0;
            AbstractDaoFactory.Instance().CreateSlot().UpdateChangingLog_Output(requestOuput, 1, 0, "gọi api cộng thành công", 1, ref returnvalueOut);

            if (returnvalueOut < 0)
            {
                responseOut = -3;
                return null;
            }

            BonusGame bonus = null;
            if (!string.IsNullOrWhiteSpace(bonnusMiniGame))
            {
                bonus = new BonusGame();
                var b = new StringBuilder(bonnusMiniGame);
                b.Replace("  ", string.Empty);
                b.Replace("{", string.Empty);
                b.Replace("}", string.Empty);

                var k = b.ToString().Split(',');
                for (int i = 0; i < k.Length - 1; i++)
                {
                    var r = k[i].Split(':');
                    if (int.Parse(r[0]) == 2)
                    {
                        bonus.DigGoldInfo = AbstractDaoFactory.Instance()
                                                              .CreateSlot()
                                                              .GetDigGoldInfo(betLogId, (int)AccountSession.AccountID);
                    }
                    else if (int.Parse(r[0]) == 1)
                    {
                        bonus.JackPot = int.Parse(r[1]);
                    }
                    else
                    {
                        bonus.CountFree = int.Parse(r[1]);
                    }
                }

            }


            var obj = new ResultGame
            {
                BonusGame = bonus,
                Prize = sumBetMoney,
                PrizeValue = sumWinMoney,
                PrizeGold = winMoneyDigGold,
                LineWin = listWin,
                IsWin = countLineWin > 0,
                Items = GenItem(resultString)
            };
            return obj;

        }

        public ResultGame BetOutputTest(long betLogId, int roomid, int serviceid, ref long responseOut)
        {
            int sumBetMoney = 0;
            int sumWinMoney = 0;
            int winMoneyDigGold = 0;
            int countLineWin = 0;
            string bonnusMiniGame = string.Empty;
            string resultString = string.Empty;
            int isFreeb = 0;

            int response = 0;

            var listWin = AbstractDaoFactory.Instance()
                                            .CreateSlot()
                                            .GetWinnerInfo_WithResultString(betLogId, "2,2,2,1,3,1,1,4,6,7,1,7,7,7,7",
                                                                            ref isFreeb, ref sumBetMoney,
                                                                            ref sumWinMoney, ref winMoneyDigGold,
                                                                            ref countLineWin, ref bonnusMiniGame,
                                                                            ref resultString, ref response);

            if (response < 0)
            {
                responseOut = -1;
                return null;
            }
            //step6
            int changeMoney = 0;
            long requestOuput = 0;
            AbstractDaoFactory.Instance()
                              .CreateSlot()
                              .InsertChangingLog_Output(betLogId, (int) AccountSession.AccountID,
                                                        AccountSession.AccountName, ref changeMoney, 0, "",
                                                        "sert api + tien " + sumWinMoney, 0, ref requestOuput);

            if (requestOuput < 0)
            {
                responseOut = -2;
                return null;
            }

            //API + tien

            //long balance=0;
            //long responseStatus=0;
            //AbstractDaoFactory.Instance().CreateSlot().SlotMachine_Award(serviceid, AccountSession.AccessToken, (int)AccountSession.AccountID, AccountSession.AccountName, ConvertRoomMoney(roomid), sumWinMoney, ref balance, betLogId,"", resultString,ref responseStatus);

            //if (responseStatus < 0)
            //{
            //    responseOut = responseStatus;
            //    return null;
            //}


            //step 8

            int returnvalueOut = 0;
            AbstractDaoFactory.Instance()
                              .CreateSlot()
                              .UpdateChangingLog_Output(requestOuput, 1, 0, "gọi api cộng thành công", 1,
                                                        ref returnvalueOut);

            if (returnvalueOut < 0)
            {
                responseOut = -3;
                return null;
            }

            BonusGame bonus = null;
            if (!string.IsNullOrWhiteSpace(bonnusMiniGame))
            {
                bonus = new BonusGame();
                var b = new StringBuilder(bonnusMiniGame);
                b.Replace("  ", string.Empty);
                b.Replace("{", string.Empty);
                b.Replace("}", string.Empty);

                var k = b.ToString().Split(',');
                for (int i = 0; i < k.Length - 1; i++)
                {
                    var r = k[i].Split(':');
                    if (int.Parse(r[0]) == 2)
                    {
                        bonus.DigGoldInfo = AbstractDaoFactory.Instance()
                                                              .CreateSlot()
                                                              .GetDigGoldInfo(betLogId, (int) AccountSession.AccountID);
                    }
                    else if (int.Parse(r[0]) == 1)
                    {
                        bonus.JackPot = int.Parse(r[1]);
                    }
                    else
                    {
                        bonus.CountFree = int.Parse(r[1]);
                    }
                }

            }


            var obj = new ResultGame
                {
                    BonusGame = bonus,
                    Prize = sumBetMoney,
                    PrizeValue = sumWinMoney,
                    PrizeGold = winMoneyDigGold,
                    LineWin = listWin,
                    IsWin = countLineWin > 0,
                    Items = GenItem(resultString)
                };
            return obj;

        }

        private Item[,] GenItem(string reslultString)
        {
            var obj = reslultString.Split(',');
            if (obj.Length == 0)
                return null;
            var items = new Item[3, 5];
            items[0, 0] = new Item() { Id =int.Parse(obj[0]) };
            items[0, 1] = new Item() { Id = int.Parse(obj[1]) };
            items[0, 2] = new Item() { Id = int.Parse(obj[2]) };
            items[0, 3] = new Item() { Id = int.Parse(obj[3]) };
            items[0, 4] = new Item() { Id = int.Parse(obj[4]) };

            items[1, 0] = new Item() { Id = int.Parse(obj[5]) };
            items[1, 1] = new Item() { Id = int.Parse(obj[6]) };
            items[1, 2] = new Item() { Id = int.Parse(obj[7]) };
            items[1, 3] = new Item() { Id = int.Parse(obj[8]) };
            items[1, 4] = new Item() { Id = int.Parse(obj[9]) };

            items[2, 0] = new Item() { Id = int.Parse(obj[10]) };
            items[2, 1] = new Item() { Id = int.Parse(obj[11]) };
            items[2, 2] = new Item() { Id = int.Parse(obj[12]) };
            items[2, 3] = new Item() { Id = int.Parse(obj[13]) };
            items[2, 4] = new Item() { Id = int.Parse(obj[14]) };
            return items;
        }

    }

    



}
